<?php
/**
 * Created by PhpStorm.
 * User: denh
 * Date: 16/5/10
 * Time: 下午5:32
 */
include_once ADMIN_PATH ."../system.back.php";

class BaseController extends Base {

    public $template = '';
    public $Js = '';     #js url路径
    public $JsFile = ''; #js磁盘路径
    public $initJs = '';
    public static $hproseHttpClient = null;

    function __construct($m = DEFAULT_CONTROLLER, $a = DEFAULT_METHOD) {
        parent::__construct();

        $this->template = ADMIN_PATH . 'templates/' . $m . '/' . $a .'.html';
        $this->JsFile = ADMIN_PATH . 'js/' . $m . '/' . $a .'.js';
        $this->Js = G_SITE_BASE . '/admin/js/' . $m . '/' . $a .'.js';

        $noLoginController = array(
            'newsApi', 'cli'
        );

        if (!$this->isLogin() and $a != 'login' and !in_array($m, $noLoginController)) {
            $this->redirect('index', 'login');
        }

        if (is_file($this->template)) {
            require_once ADMIN_PATH . '../tools/tools.page.php'; #分页类
        }
    }

    function __destruct() {
        if (is_file($this->JsFile) and $this->isLogin()) {
            echo "\n".'<script language="JavaScript">' . "\n";
            require_once $this->JsFile;
            echo "\n" . '</script>';
        }
    }

    function isLogin() {
        if (!empty($_SESSION['user']) or (isset($_GET['cli']) and $_GET['cli'] == '27tjdhs2323')) {
            return true;
        }
        return false;
    }

    function url($m, $a, $param = array()) {
        $url = G_SITE_BASE . "/admin/index.php?m=$m&a=$a";
        if ($param) {
            $q = http_build_query($param);
            $url = $url.'&'.$q;
        }
        return $url;
    }

    function redirect($m, $a, $param = array()) {
        $url = $this->url($m, $a, $param);
        header("Location:".$url);
        exit();
    }

    function request($str, $default = '', $isMust = false) {
        $value = array_key_exists($str, $_REQUEST) ? $_REQUEST[$str] : $default;
        if (!$value and $isMust) {
            $str = sprintf("%s 为必填字段", $str);
            if (is_file($this->template)) {
                $this->errorPage($str);
            } else {
                $this->jsonOutPut(-1, $str);
            }
        }
        if (!is_array($value)) $value = trim($value);
        return $value;
    }

    function jsonOutPut($status, $msg='', $data=array()) {
        $result = new \BoOutPuts($status, $msg, $data);
        exit(json_encode($result));
    }

    function initScript($data = array()) {
        $data = json_encode($data);

        $initJs = "<script language='javascript'>
        function get_init_data(){return %s}\n</script>\n";

        $this->initJs = sprintf($initJs, $data);
    }
    function successPage($Msg, $jumpUrl = '') {
        require ADMIN_PATH . 'templates/common/successPage.html';
        exit();
    }
    function errorPage($errMsg, $jumpUrl = '') {
        require ADMIN_PATH . 'templates/common/errorPage.html';
        exit();
    }

    function get_js() {
        if (is_file($this->JsFile)) {
            echo sprintf("<script src= '%s' language='JavaScript'></script>", $this->Js);
        }
    }

    function load_hprose() {
        require_once ADMIN_PATH . 'sdk/hprose-php-master/src/Hprose.php';
        if (null == self::$hproseHttpClient) {
            self::$hproseHttpClient = new \HproseHttpClient(BACK_INTERFACE_BASE_URL);
        }
    }

    #获取app接口token
    function tourist_login() {
        if (G_SERV_ID == 14) {
            $_SESSION['app_token'] = '0cf452fb28fb474b9ced6eedfb1dadfd8a7b85edf76b4cbc9125d5bf8c2b1cd4';
        } else {
            $_SESSION['app_token'] = 'fd45e92edbb641548a9c2d036bc8e66c4756acfa05544515b8aff6a8b4f02e52';
        }
    }

    #多进程命令行执行
    function run_cli($type, $id) {
        $doUser = new Do用户();
        $max_id = $doUser->fields("max(id)")->getOneField();
        $进程数 = ceil($max_id / NewsCount_PerProcess);
        for ($i = 0; $i <= $进程数; $i++) {
            $limit = $i * NewsCount_PerProcess;
            $command = "nohup php index.php cli $type $id $limit >> news.log &";
            exec($command);
            logger()->logInfo("群发消息命令：" . $command);
        }
    }



}

class BoOutPuts{
    public $result=-1;
    public $datas='';
    public $message='错误';

    public function __construct($result=-1, $message='',$datas='')
    {
        $this->datas = $datas;
        $this->message = $message;
        $this->result = $result;
    }

}